﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Web;
using System.Web.Services;
using System.Web.Script;
using System.Web.Script.Services;
using System.Drawing;
using System.Data;
using System.Data.SQLite;
using SqliteWrapper;
using SqliteWrapper.Procedures;
using SqliteWrapper.Query;
using BartonExtension;
using Core;

/// <summary>
/// iReaderWebSrvs 的摘要描述
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務，請取消註解下列一行。
[System.Web.Script.Services.ScriptService]
public class iReaderWebSrvs : System.Web.Services.WebService {

    public iReaderWebSrvs () {

        //如果使用設計的元件，請取消註解下列一行
        //InitializeComponent(); 
    }

    //Book Processor
    [WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public string AddBook(string tit,string author,string sPath,string desc,int aid,int cid,string thumb)  
    {
        NonQueryWrapper iReaderWrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        return iReaderWrapper.AddBook(tit,author, sPath, desc, aid, cid,thumb).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string BatchImportBooks(int categoryId, List<string> tit, List<string> desc, List<string> srcPath,List<string> thumbs)
    {
        NonQueryWrapper iReaderWrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        return iReaderWrapper.BatchImportBooks(categoryId, tit, desc, srcPath,thumbs).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string DeleteBook(long bid)
    {
        NonQueryWrapper iReaderWrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        return iReaderWrapper.DeleteBook(bid).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string UpdateBook(long bid, string tit,string author, string desc, long aid, long cid,string sPath,string thumb,float version)
    {
        NonQueryWrapper iReaderWrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        return iReaderWrapper.UpdateBook(tit,author, sPath, desc, aid, cid,thumb,version).ToJson();
    }

    //Album Processor
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string AddAlbum(string tit,string author, string desc, int cid, string bookSrcs,string thumb)
    {
        NonQueryWrapper iReaderWrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        return iReaderWrapper.AddAlbum(tit,author, desc,cid,bookSrcs,thumb).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string DeleteAlbum(bool deleteBooks, long albumId)
    {
        NonQueryWrapper iReaderWrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        return iReaderWrapper.DeleteAlbum(deleteBooks, albumId).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string UpdateAlbum(long albumId,string name,string author,string desc,string thumb,bool reIssue)
    {
        NonQueryWrapper iReaderWrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        return iReaderWrapper.UpdateAlbum(albumId, name,author, desc,thumb, reIssue).ToJson();
    }

    //Category Processor
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string AddCategroy(string tit, string desc)
    {
        NonQueryWrapper iReaderWrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        return iReaderWrapper.AddCategory(tit, desc).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string DeleteCategory(long cid,bool deleteBook)
    {
        NonQueryWrapper iReaderWrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        return iReaderWrapper.DeleteCategory(cid, deleteBook).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string UpdateCategory(long cid, string tit, string desc)
    {
        NonQueryWrapper iReaderWrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        return iReaderWrapper.UpdateCategory(cid, tit, desc).ToJson();
    }

    //Book Query Processor
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string QueryBook(string q_tit, long cid, long aid, int startIndex, int length, string startTime, string endTime,string orderJsonObject)
    {
        QueryWrapper iReaderWrapper = QueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        QueryOrder order = orderJsonObject.FromJson<QueryOrder>();
        List<SQLiteParameter> collection = new List<SQLiteParameter>();
        if(q_tit != null)
            collection.Add(new SQLiteParameter("Title", DbType.String) { Value = q_tit });
        if(aid >= -1)
            collection.Add(new SQLiteParameter("AlbumId", DbType.Int64) { Value = aid });
        if(cid > 0)
            collection.Add(new SQLiteParameter("CategoryId", DbType.Int64) { Value = cid });
        return iReaderWrapper.GetBooks(collection, startIndex, length, startTime.toDateTime(), endTime.toDateTime(),order).ToJson();
    }

    //Album Query Processor
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string QueryAlbum(string q_tit, int startIndex, int length, string startTime, string endTime, string orderJsonObject)
    {
        QueryWrapper iReaderWrapper = QueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        QueryOrder order = orderJsonObject.FromJson<QueryOrder>();
        List<SQLiteParameter> collection = new List<SQLiteParameter>();
        if (q_tit != null)
            collection.Add(new SQLiteParameter("Title", DbType.String) { Value = q_tit });
        return iReaderWrapper.GetAlbums(collection,startIndex,length,startTime.toDateTime(),endTime.toDateTime(),order).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetAlbumByID(long id)
    {
        QueryWrapper iReaderWrapper = QueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        List<SQLiteParameter> collection = new List<SQLiteParameter>();
        collection.Add(new SQLiteParameter("ID", DbType.UInt64) { Value = id });
        return iReaderWrapper.GetAlbums(collection, null, null, null, null,null).ToJson();
    }

    //Category Query Processor
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string QueryCategory(string q_tit, int startIndex, int length, string startTime, string endTime, string orderJsonObject)
    {
        QueryWrapper iReaderWrapper = QueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        QueryOrder order = orderJsonObject.FromJson<QueryOrder>();
        List<SQLiteParameter> collection = new List<SQLiteParameter>();
        if(q_tit != null)
            collection.Add(new SQLiteParameter("Title", DbType.String) { Value = q_tit });
        return iReaderWrapper.GetCategories(collection,startIndex,length,startTime.toDateTime(),endTime.toDateTime(),order).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetCategoryByID(long id)
    {
        QueryWrapper iReaderWrapper = QueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        List<SQLiteParameter> collection = new List<SQLiteParameter>();
        collection.Add(new SQLiteParameter("ID", DbType.UInt64) { Value = id });
        return iReaderWrapper.GetCategories(collection, null, null, null, null,null).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Query(string query)
    {
        QueryWrapper iReaderWrapper = QueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        return iReaderWrapper.GetData(query).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Download(long id)
    {
        NonQueryWrapper iReaderWrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        return iReaderWrapper.DownloadBook(id).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string QueryComment(long bid,int startIndex,int length,string startTime,string endTime,string orderJsonObject)
    {
        QueryWrapper iReaderWrapper = QueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        QueryOrder order = orderJsonObject.FromJson<QueryOrder>();
        List<SQLiteParameter> collection = new List<SQLiteParameter>();
        collection.Add(new SQLiteParameter("bid", DbType.Int64) { Value = bid });
        return iReaderWrapper.GetComments(collection, startIndex, length, startTime.toDateTime(), endTime.toDateTime(), order).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string AddComment(string commentJsonObject)
    {
        NonQueryWrapper iReaderWrapper = NonQueryWrapper.Create(Server.MapPath(ConfigurationExtensions.ValueOfConfig("DBPath")));
        Comment comment = commentJsonObject.FromJson<Comment>();
        return iReaderWrapper.AddComment(comment).ToJson();
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetServerStatus(object user)
    {
        ServerInfo hostInfo = new ServerInfo()
        {
            Host = ConfigurationExtensions.ValueOfConfig("Host"),
            ClientVersion = 1.0,
            MustUpdateUnderVersion = 0.0,
            StartUpCovers = null,
            SystemNotificationMessage = "欢迎使用",
            UpdateUrl = null
        };
        return hostInfo.ToJson();
    }
}
